var express = require('express');
var userdb = require('../db/user.db');
var logger = require('../logger');
var getRouter = express.Router();
var postRouter = express.Router();

/* 渲染登录页面 */
getRouter.get('/', function (req, res, next) {
    res.render('login', { flag: 0, info: ' ' });
});


/* 处理登录请求 */
postRouter.post('/', function (req, res) {
    userdb.queryuser(req.body.username, req.body.password, (err) => {
        if (err) {
            logger.log('error', err);
            res.render('login', { flag: 1, info: err });
        } else
        {
            logger.log('user', req.body.username);
            res.cookie('authorized', req.body.username);
            res.render('browser');
        }
    })
})

exports.get = getRouter;
exports.post = postRouter;